#ifndef CONNPOOL_H_
#define CONNPOOL_H_

#include "Common.h"
#include "ConfManager.h"

class Socket
{
    public:
        Socket():fd(0),ip(""),port(0),isInEpoll(false){}

        int32 fd;
        std::string ip;
        int32 port;
        bool isInEpoll;
};

class ConnPool
{
    private:

        static pthread_mutex_t _mutex_init;

        static ConnPool *_instance;

        pthread_mutex_t _mutex;

        pthread_cond_t _notEmptyCond;

        int32 _epollFd;

        int32 _listenFd;

        int32 _port;

        bool _isRunning;

        int32 _socketPoolSize;

        int32 _socketAccpetSize;

        int32 _epollTimeOut;

        struct epoll_event *_pEvents;

        std::queue<int32> _socketContainer;

        std::map<int32, Socket> _socketInfoContainer;

        ConnPool();

        ConnPool(int32 port, int32 socketPollSize, int32 socketAcceptSize, int32 epollTimeOut);

        bool _EpollAdd(int32 fd);

        bool _EpollMod(int32 fd);

        bool _EpollDel(int32 fd);

        void _CloseSocket(Socket socket);

        void _KeepSocket(Socket socket);

        bool _PushSocket(Socket socket);

    public:

        static ConnPool *Instance();

        ~ConnPool();

        bool IsRunning();

        void Start();

        bool CheckSocket();

        void Stop();

        bool PopSocket(Socket &socket);

        void ResetSocket(Socket socket, int32 keepAlive);

        int32 ActiveSocketSize();

        int32 EpollSocketSize();
};

#endif
